package com.lyw.mine

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.LogUtils
import com.lu.library.kv.KVDelegate
import com.lyw.login_api.LoginConstants
import com.lyw.login_api.bean.UserInfo

object UserInfoManager {



    private var userInfoStr by KVDelegate(key= LoginConstants.KEY_USER_INFO, def="")
    private var _userInfo : MutableLiveData<UserInfo> = MutableLiveData(if(userInfoStr.isEmpty()){
        UserInfo()
    }else{
        GsonUtils.fromJson(userInfoStr, UserInfo::class.java)})

    var userInfo :LiveData<UserInfo> = _userInfo


    fun updateUserInfo(block:UserInfo.()->UserInfo){


        LogUtils.d("updateUserInfo")
        _userInfo.value = block(_userInfo.value!!)
        userInfoStr =  GsonUtils.toJson( _userInfo.value)

    }

}